<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <!-- <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item
        label="姓名"
        prop="name"
      >
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="联系方式" prop="contactInfo">
        <el-input
          v-model="queryParams.contactInfo"
          placeholder="请输入联系方式"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item
        label="毕业学校"
        prop="school"
      >
        <el-input
          v-model="queryParams.school"
          placeholder="请输入毕业学校"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="学历"
        prop="grade"
      >
        <el-input
          v-model="queryParams.grade"
          placeholder="请输入学历"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="专业"
        prop="major"
      >
        <el-input
          v-model="queryParams.major"
          placeholder="请输入专业"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="所在省份"
        prop="province"
      >
        <el-input
          v-model="queryParams.province"
          placeholder="请输入所在省份"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="所在城市"
        prop="city"
      >
        <el-input
          v-model="queryParams.city"
          placeholder="请输入所在城市"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="所在县区"
        prop="district"
      >
        <el-input
          v-model="queryParams.district"
          placeholder="请输入所在县区"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="详细地址" prop="location">
        <el-input
          v-model="queryParams.location"
          placeholder="请输入详细地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="照片" prop="imageUrl">
        <el-input
          v-model="queryParams.imageUrl"
          placeholder="请输入照片"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item
        label="可授年级"
        prop="studentGrade"
      >
        <el-input
          v-model="queryParams.studentGrade"
          placeholder="请输入可授年级"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="可授科目"
        prop="subjects"
      >
        <el-input
          v-model="queryParams.subjects"
          placeholder="请输入可授科目"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item
        label="薪资要求"
        prop="salaryRequirements"
      >
        <el-input
          v-model="queryParams.salaryRequirements"
          placeholder="请输入薪资要求"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <!-- <el-form-item label="创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="更新时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择更新时间">
        </el-date-picker>
      </el-form-item> -->
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >搜索</el-button>
        <el-button
          icon="el-icon-refresh"
          size="mini"
          @click="resetQuery"
        >重置</el-button>
      </el-form-item>
    </el-form>

    <el-row
      :gutter="10"
      class="mb8"
    >
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          v-if="!isTutorrequest"
          @click="handleAdd"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          v-if="!isTutorrequest"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          v-if="!isTutorrequest"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="tutorsList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        align="center"
      />
      <el-table-column
        label="姓名"
        align="center"
        prop="name"
      />
      <el-table-column
        label="性别"
        align="center"
        prop="gender"
      />
      <el-table-column
        label="毕业学校"
        align="center"
        prop="school"
      />
      <el-table-column
        label="学历"
        align="center"
        prop="grade"
      />
      <el-table-column
        label="专业"
        align="center"
        prop="major"
      />
      <el-table-column
        label="所在省份"
        align="center"
        prop="province"
      />
      <el-table-column
        label="所在城市"
        align="center"
        prop="city"
      />
      <el-table-column
        label="所在县区"
        align="center"
        prop="district"
      />
      <el-table-column
        label="详细地址"
        align="center"
        prop="location"
      />
      <el-table-column
        label="照片"
        align="center"
        prop="imageUrl"
      >
        <template slot-scope="scope">
          <ImagePreview :src="scope.row.imageUrl"></ImagePreview>
        </template>
      </el-table-column>
      <el-table-column
        label="可授年级"
        align="center"
        prop="studentGrade"
      />
      <el-table-column
        label="可授科目"
        align="center"
        prop="subjects"
      />
      <el-table-column
        label="家教经验"
        align="center"
        prop="experience"
        width="120"
      />
      <el-table-column
        label="薪资要求"
        align="center"
        prop="salaryRequirements"
      />
      <el-table-column
        label="联系方式"
        align="center"
        prop="contactInfo"
        width="120"
      />
      <el-table-column
        label="自我描述"
        align="center"
        prop="description"
        width="150"
      />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createdAt"
        width="150"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="更新时间"
        align="center"
        prop="updatedAt"
        width="150"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="120"
        align="center"
        class-name="small-padding fixed-width"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-if="isTutorrequest"
          >聊天</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="!isTutorrequest"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            style="color: red;"
            icon="el-icon-delete"
            v-if="!isTutorrequest"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改教师信息对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="700px"
      append-to-body
    >
      <div style="height: 65vh;overflow: auto;">
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          label-width="80px"
          :inline="true"
        >
          <!-- <el-form-item label="用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户ID" />
        </el-form-item> -->
          <h3 style="font-weight: 900">个人信息</h3>
          <el-form-item
            label="姓名"
            prop="name"
          >
            <el-input
              v-model="form.name"
              placeholder="请输入姓名"
            />
          </el-form-item>
          <el-form-item
            label="性别"
            prop="gender"
          >
            <el-select
              v-model="form.gender"
              placeholder="请选择性别"
              clearable
            >
              <el-option
                label="男"
                value="男"
              ></el-option>
              <el-option
                label="女"
                value="女"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="毕业学校"
            prop="school"
          >
            <el-input
              v-model="form.school"
              placeholder="请输入毕业学校"
            />
          </el-form-item>
          <el-form-item
            label="学历"
            prop="grade"
          >
            <!-- <el-input v-model="form.grade" placeholder="请输入学历" /> -->
            <!-- educationLevels -->
            <el-select
              v-model="form.grade"
              placeholder="请输入学历"
              clearable
            >
              <el-option
                v-for="item in educationLevels"
                :key="item"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="专业"
            prop="major"
          >
            <el-input
              v-model="form.major"
              placeholder="请输入专业"
            />
          </el-form-item>
          <el-form-item
            label="所在省份"
            prop="province"
          >
            <el-select
              v-model="form.province"
              placeholder="请输入省"
              clearable
              @change="provinceChange"
            >
              <el-option
                v-for="item in province"
                :key="item.Id"
                :label="item.Name"
                :value="item.Name"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="所在城市"
            prop="city"
          >
            <el-select
              v-model="form.city"
              placeholder="请输入所在城市"
              clearable
              @change="cityChange"
            >
              <el-option
                v-for="item in city"
                :key="item.Id"
                :label="item.Name"
                :value="item.Name"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="所在县区"
            prop="district"
          >
            <el-select
              v-model="form.district"
              placeholder="请输入区/县"
              clearable
            >
              <el-option
                v-for="item in district"
                :key="item.Id"
                :label="item.Name"
                :value="item.Name"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="详细地址"
            prop="location"
          >
            <el-input
              v-model="form.location"
              placeholder="请输入详细地址"
            />
          </el-form-item>
          <el-form-item
            label="照片"
            prop="imageUrl"
          >
            <!-- <el-input v-model="form.imageUrl" placeholder="请输入照片" /> -->
            <ImageUpload
              v-model="form.imageUrl"
              :limit="1"
            ></ImageUpload>
          </el-form-item>
          <h3 style="font-weight: 900;">专业技能</h3>
          <el-form-item
            label="可授年级"
            prop="studentGrade"
          >
            <!-- <el-input v-model="form.studentGrade" placeholder="请输入可授年级" /> -->
            <el-select
              v-model="form.studentGrade"
              placeholder="请选择年级"
              clearable
            >
              <el-option
                v-for="grade in grades"
                :key="grade"
                :label="grade"
                :value="grade"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item
            label="可授科目"
            prop="subjects"
          >
            <el-input
              v-model="form.subjects"
              placeholder="请输入可授科目"
            />
          </el-form-item>
          <el-form-item
            label="家教经验"
            prop="experience"
          >
            <el-input
              v-model="form.experience"
              type="textarea"
              placeholder="请输入内容"
            />
          </el-form-item>
          <el-form-item
            label="薪资要求"
            prop="salaryRequirements"
          >
            <el-input
              v-model="form.salaryRequirements"
              placeholder="请输入薪资要求"
            />
          </el-form-item>
          <el-form-item
            label="联系方式"
            prop="contactInfo"
          >
            <el-input
              v-model="form.contactInfo"
              placeholder="请输入联系方式"
            />
          </el-form-item>
          <el-form-item
            label="自我描述"
            prop="description"
          >
            <el-input
              v-model="form.description"
              type="textarea"
              placeholder="请输入内容"
            />
          </el-form-item>
          <!-- <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item> -->
        </el-form>
      </div>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="submitForm"
        >确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listTutors,
  getTutors,
  delTutors,
  addTutors,
  updateTutors,
} from '@/api/system/tutors'
import ImageUpload from '@/components/ImageUpload'
import ImagePreview from '@/components/ImagePreview'
import { listArea } from '@/api/system/tutorrequest'
export default {
  name: 'Tutors',
  components: { ImageUpload, ImagePreview },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      isTutorrequest: 1,
      // 教师信息表格数据
      tutorsList: [],
      // 弹出层标题
      title: '',
      province: [],
      city: [],
      district: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        name: null,
        gender: null,
        school: null,
        grade: null,
        major: null,
        province: null,
        city: null,
        district: null,
        location: null,
        imageUrl: null,
        studentGrade: null,
        subjects: null,
        experience: null,
        salaryRequirements: null,
        contactInfo: null,
        description: null,
        createdAt: null,
        updatedAt: null,
      },
      educationLevels: [
        '小学',
        '初中',
        '高中',
        '中专',
        '大专',
        '本科',
        '硕士研究生',
        '博士研究生',
        '博士后',
        '其他',
      ],
      grades: [
        '小学一年级',
        '小学二年级',
        '小学三年级',
        '小学四年级',
        '小学五年级',
        '小学六年级',
        '初中一年级',
        '初中二年级',
        '初中三年级',
        '高中一年级',
        '高中二年级',
        '高中三年级',
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: '用户ID不能为空', trigger: 'blur' },
        ],
        name: [{ required: true, message: '姓名不能为空', trigger: 'blur' }],
        gender: [{ required: true, message: '性别不能为空', trigger: 'blur' }],
        school: [
          { required: true, message: '毕业学校不能为空', trigger: 'blur' },
        ],
        grade: [{ required: true, message: '学历不能为空', trigger: 'blur' }],
        province: [
          { required: true, message: '所在省份不能为空', trigger: 'blur' },
        ],
        city: [
          { required: true, message: '所在城市不能为空', trigger: 'blur' },
        ],
        district: [
          { required: true, message: '所在县区不能为空', trigger: 'blur' },
        ],
        imageUrl: [
          { required: true, message: '照片不能为空', trigger: 'blur' },
        ],
        studentGrade: [
          { required: true, message: '可授年级不能为空', trigger: 'blur' },
        ],
        subjects: [
          { required: true, message: '可授科目不能为空', trigger: 'blur' },
        ],
        experience: [
          { required: true, message: '家教经验不能为空', trigger: 'blur' },
        ],
        salaryRequirements: [
          { required: true, message: '薪资要求不能为空', trigger: 'blur' },
        ],
        contactInfo: [
          { required: true, message: '联系方式不能为空', trigger: 'blur' },
        ],
      },
    }
  },
  async created() {
    let res = await listArea({ level: 0 })
    this.province = res.rows
    this.isTutorrequest = this.$store.getters.user.userType == 1 ? false : true
    this.queryParams.userId = !this.isTutorrequest
      ? this.$store.state.user.id
      : null
    this.getList()
  },
  methods: {
    handleView(row) {
      let chats = this.$store.getters.chats
      let chat = chats.find((v) => {
        return v.user1Id === row.userId || v.user2Id === row.userId
      })
      if (chat) {
        this.$router.push({
          path: '/teacher/websocket',
          query: {
            chatId: chat.chatId,
          },
        })
        return
      }
      let chatObj = {
        name:"chat",
        user1Id: row.userId ,
        user2Id:  this.$store.state.user.id ,
        createdAt:dayjs().format('YYYY-MM-DD HH:mm:ss'),
      }
      this.$websocket.sendWebsocket(chatObj)
      this.$router.push({
          path: '/teacher/websocket',
          query: {
         
          },
        })
    },
    async provinceChange() {
      let data = this.province.find((v) => v.Name === this.form.province)
      let res = await listArea({ parentId: data.Id })
      this.city = res.rows
      this.$set(this.form, 'city', null)
      this.$set(this.form, 'district', null)
    },
    async cityChange() {
      let data = this.city.find((v) => v.Name === this.form.city)
      let res = await listArea({ parentId: data.Id })
      this.district = res.rows
      this.$set(this.form, 'district', null)
    },
    /** 查询教师信息列表 */
    getList() {
      this.loading = true
      listTutors(this.queryParams).then((response) => {
        this.tutorsList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: this.$store.getters.id,
        name: this.$store.getters.user.nickName || this.$store.getters.name,
        // phone: this.$store.getters.user.phonenumber,
        gender: '女',
        school: null,
        grade: null,
        major: null,
        province: null,
        city: null,
        district: null,
        location: null,
        imageUrl: '',
        studentGrade: null,
        subjects: null,
        experience: null,
        salaryRequirements: null,
        contactInfo: this.$store.getters.user.phonenumber,
        description: null,
        createdAt: null,
        updatedAt: null,
      }
      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm')
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加教师信息'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getTutors(id).then((response) => {
        this.form = response.data
        this.open = true
        this.title = '修改教师信息'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateTutors(this.form).then((response) => {
              this.$modal.msgSuccess('修改成功')
              this.open = false
              this.getList()
            })
          } else {
            addTutors(this.form).then((response) => {
              this.$modal.msgSuccess('新增成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal
        .confirm('是否确认删除教师信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delTutors(ids)
        })
        .then(() => {
          this.getList()
          this.$modal.msgSuccess('删除成功')
        })
        .catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        'system/tutors/export',
        {
          ...this.queryParams,
        },
        `tutors_${new Date().getTime()}.xlsx`
      )
    },
  },
}
</script>
